package com.ben.smm.broadcast;

import java.util.Date;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.SmsMessage;
import android.util.Log;

import com.ben.smm.util.SMMConstants;
import com.ben.smm.util.SMMDialogUtil;

/**
 * 短信送达回执
 * 
 * @author lixuanbin
 */
public class DeliveredReceiver extends BroadcastReceiver {
	private int deliveryCount = 0;

	@Override
	public void onReceive(Context context, Intent intent) {
		String action = intent.getAction();
		if (action != null && SMMConstants.SMS_DELIVERED.equals(action)) {
			deliveryCount += 1;
			Object pdu = (Object) intent.getExtras().get("pdu");
			if (pdu != null) {
				SmsMessage receipt = SmsMessage.createFromPdu((byte[]) pdu);
				if (receipt.isStatusReportMessage()) {
					// TODO 处理发送状态报告
					Date deliveredDate = new Date(receipt.getTimestampMillis());
					Log.d(SMMConstants.LOG_TAG, deliveredDate.toString());
					Log.d(SMMConstants.LOG_TAG,
							String.valueOf(receipt.getStatus()));
					Log.d(SMMConstants.LOG_TAG,
							String.valueOf(receipt.getStatusOnIcc()));
					Log.d(SMMConstants.LOG_TAG,
							String.valueOf(receipt.getIndexOnIcc()));
				}
				SMMDialogUtil.simpleOkDialog(context, "发送成功",
						"class name: " + pdu.getClass().toString()
								+ "\ntoString: " + pdu.toString());
			} else {
				SMMDialogUtil
						.simpleOkDialog(context, "pdu为null", "pdu == null");
			}
			context.unregisterReceiver(DeliveredReceiver.this);
		}
	}
}